// Async / Await 语法糖
function ajax(url) {
    return new Promise((resolve, reject) =>{
        var xhr = new XMLHttpRequest()
        xhr.open('GET', url)
        xhr.responseType = 'json'
        xhr.onload = function () {
            if (this.status === 200) {
                resolve(this.response)
            } else {
                reject(new Error(this.statusText))
            }
        }
    })
}

async function main () {
    try {
        const users = await ajax('/api/users.json')
        console.log(users)

        const posts = await ajax('/api/posts.json')
        console.log(posts)

        const urls = await ajax('/api/urls.json')
        console.log(urls)
    } catch (error) {
        console.log(error)
    }
}

const promise = main()

promise.then(() => {
    console.log('all completed')
})